package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService{

    //注入redis
    @Autowired
    private StringRedisTemplate redisTemplate;
    //注入
    @Autowired
    private IStrategyService strategyService;
    //阅读数加一
    @Override
    public void viewnumIncrease(String sid, int num) {
        //拿对象
        StrategyStatisVO vo=this.getStrategyStatisVo(sid);

        //加1
        vo.setViewnum(vo.getViewnum()+num);
        //更新
        this.setStrategyStatisVo(vo);
    }

    //评论数加一
    @Override
    public void replynumIncrease(String sid, int num) {
        //调用方法拿对象
        StrategyStatisVO vo=this.getStrategyStatisVo(sid);
        //加一
        vo.setReplynum(vo.getReplynum()+num);
        //调用方法添加
        this.setStrategyStatisVo(vo);
    }
    //收藏
    @Override
    public boolean favor(String sid, String userId) {
        //拼接用户攻略收藏id集合的key
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(userId);
        //获取redis上的的攻略id集合
        List<String> sidlist = new ArrayList<>();
        if (redisTemplate.hasKey(key)){
            //通过key拿攻略id集合
            String sidlistStr= redisTemplate.opsForValue().get(key);
            //解析json集合,json字符串  集合中元素类型
            //转换为java中list集合
            sidlist = JSON.parseArray(sidlistStr, String.class);
        }
        //boolean fag=false;
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        if (sidlist.contains(sid)){
            //包含  收藏数减一  移除该sid
            vo.setFavornum(vo.getFavornum()-1);
            sidlist.remove(sid);
           // return true;
        }else {
            //不包含  收藏书加一  添加到集合中
            vo.setFavornum(vo.getFavornum()+1);
            sidlist.add(sid);
        }


        //最后更新sid攻略集合   更新 vo
        redisTemplate.opsForValue().set(key, JSON.toJSONString(sidlist));
        this.setStrategyStatisVo(vo);
        //当是收藏执行到这里时已经有了这个sid 就是true
        return sidlist.contains(sid);
    }
    //用来做收藏图标的显示和不显示
    @Override
    public List<String> getUserStrategiesFavor(String userId) {
       if (!StringUtils.hasLength(userId)){
           return Collections.EMPTY_LIST;
       }

        //拼接用户攻略收藏id集合的key
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(userId);
        //获取redis上的的攻略id集合
        List<String> sidlist = new ArrayList<>();
        if (redisTemplate.hasKey(key)){
            //通过key拿攻略id集合
            String sidlistStr= redisTemplate.opsForValue().get(key);
            //解析json集合,json集合类型字符串  集合中元素类型
            //转换为java中list集合
            sidlist = JSON.parseArray(sidlistStr, String.class);
        }



        return sidlist;
    }


    @Override
    public boolean strategyThumbup(String sid, String uid) {
        //构建key
        String key = RedisKeys.STRATEGY_THUMB.join(uid, sid);
        //没有key就可以顶
        if (!redisTemplate.hasKey(key)){
            StrategyStatisVO vo = this.getStrategyStatisVo(sid);
            vo.setThumbsupnum(vo.getThumbsupnum()+1);
            this.setStrategyStatisVo(vo);
            //设置有效时间是今天最后一秒减当前时间
            Date now = new Date();
            Date endDate = DateUtil.getEndDate(now);
            long time = DateUtil.getDateBetween(now, endDate);
            //设进redis
            redisTemplate.opsForValue().set(key,"1",time, TimeUnit.SECONDS);
            return true;

        }

        //key存在就是false不可以再顶了
        return false;
    }

    //初始化时判断vo对象是否存在
    @Override
    public boolean isStrategyVoExists(String id) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(id);
        //判断vo对象是否已经存在
        return redisTemplate.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStrategyVoByPattern(String pattern) {
        ArrayList<StrategyStatisVO> list = new ArrayList<>();
        //key:*
        String key = RedisKeys.STRATEGY_STATIS_VO.join(pattern);
        //查出key集合
        Set<String> keys = redisTemplate.keys(key);
        if (keys!=null&&keys.size()>0){
            for (String k : keys) {
                String strategyStatisVO= redisTemplate.opsForValue().get(k);
                StrategyStatisVO vo = JSON.parseObject(strategyStatisVO, StrategyStatisVO.class);
                list.add(vo);
            }
        }
        return list;

    }

    //获取redisvo对象
    @Override
    public StrategyStatisVO getStrategyStatisVo(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //key存在
        StrategyStatisVO vo=null;
        if (redisTemplate.hasKey(key)){
            //通过key拿value 得到json类型的字符串
            String str = redisTemplate.opsForValue().get(key);
            //json字符串转换成对象
            vo= JSON.parseObject(str,StrategyStatisVO.class);
        }else{
            //不存在
            vo=new StrategyStatisVO();
            //拿攻略对象做数据初始化
            Strategy strategy = strategyService.get(sid);
            //初始化
            BeanUtils.copyProperties(strategy,vo);
            //设置攻略id
            vo.setStrategyId(strategy.getId());

            redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
        }
        return vo;
    }
    //设置vo对象到redis
    @Override
    public void setStrategyStatisVo(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));

    }


}
